from myelin.Ns import MYELINS
from Reset import Form


class Form(Form):
    
    def createInstance(self, context, node):
        defaults = context.defaults        
        eventData = context.event.data
        config = eventData.config
        
        map = context.uriMapper.apply
        
        #model = eventData.model
        params = eventData.params
        errors = eventData.errors
        
        e_get = errors.get
        
        nxa = node.xml_append
        nxc = node.xml_create_element
        
        paramDefaults = self.defaults
        
        curNode = self.createMethodOptions(
            context,
            nxa(
                nxc(
                    u'a:method', MYELINS
                )            
            ),
            (context.nodeType.actions.get(self.defaultSubmitAction) or {}).keys()
        ) 
        e = e_get('method')
        if e:
            curNode.error = e
        
        curNode = nxa(
            nxc(
                u'a:query', MYELINS,
                content = params.get('query') or None
            )
        )
        e = e_get('query')
        if e:
            curNode.error = e
        
        #locale
        curNode = nxa(
            nxc(
                u'a:language', MYELINS,
                attributes = {
                    u'selected' : params.get('language') or u'',
                }
            )
        )
        e = e_get('language')
        if e:
            curNode.error = e
            
        #curNode = nxa(
        #    nxc(
        #        u'a:region', MYELINS,
        #        attributes = {
        #            u'selected' : params.get('region') or u'',
        #        }
        #    )
        #)
        #e = e_get('region')
        #if e:
        #    curNode.error = e            
        
        #value = context.session.dateTime
        #if params['efdFlag']:
        #    curNode = nxa(
        #        nxc(
        #            u'a:effectiveDate', MYELINS,
        #            attributes = {
        #                u'selected' : u'1',
        #                u'year' : unicode(params['efdYr']),
        #                u'mon'  : unicode(params['efdMon']),
        #                u'start-year' : unicode(
        #                    getattr(
        #                        defaults,
        #                        'default_search_start_year'
        #                    ) or paramDefaults['search-start-year']
        #                ),
        #                u'end-year' : unicode(value.year),
        #            }
        #        )
        #    )
        #else:
        #    dsy = unicode(
        #        getattr(
        #            defaults,
        #            'default_search_start_year',
        #            paramDefaults['search-start-year']
        #        )
        #    )
        #    curNode = nxa(
        #        nxc(
        #            u'a:effectiveDate', MYELINS,
        #            attributes = {
        #                u'year' : dsy,
        #                u'mon'  : u'1',
        #                u'start-year' : dsy,
        #                u'end-year' : unicode(value.year),
        #            }
        #        )
        #    )
        #e = e_get('efdFlag') or e_get('efdYr') or e_get('efdMon')
        #if e:
        #    curNode.error = e
        
        self.createTextIndexUriOptions(
            context,
            nxa(
                nxc(
                    u'a:textindex-uri', MYELINS,
                )
            ),
            params['textindex-uri']
        )
        
        # count select
        curNode = nxa(
            nxc(
                u'a:count', MYELINS,
                attributes = {
                    u'min'      : config.params.get((None,'default-count-min')) or paramDefaults['search-count-min'],
                    u'max'      : config.params.get((None,'default-count-max')) or paramDefaults['search-count-max'],
                    u'step'     : config.params.get((None,'default-count-step')) or paramDefaults['search-count-step'],
                    u'selected' : unicode(params['count']) or paramDefaults['search-count'],
                }
            )
        )
        e = e_get('count')
        if e:
            curNode.error = e
        
        curNode = nxa(
            nxc(
                u'a:page', MYELINS,
                content = u'0'
            )
        )
        e = e_get('count')
        if e:
            curNode.error = e
        
        #curNode = nxa(
        #    nxc(
        #        u'a:sort-by', MYELINS,
        #        attributes = {
        #            u'selected'     : params.has_key('sort-by') and unicode(params['sort-by']) or u'0',
        #        }
        #    )
        #)
        #e = e_get('sort-by')
        #if e:
        #    curNode.error = e
        #
        #curNode = nxa(
        #    nxc(
        #        u'a:sort-direction', MYELINS,
        #        attributes = {
        #            u'selected'     : params.has_key('sort-direction') and unicode(params['sort-direction']) or u'0'
        #        }
        #    )
        #)
        
        #nxa(
        #    nxc(
        #        u'a:publish-type', MYELINS,
        #        attributes = {
        #            u'selected' : u'0',
        #        }
        #    )
        #)
        
        # publishPeriod
        
        # type
        
        return node
    
    
# cleanup